package show.me.the.money.util.exp.standard;

import java.util.List;
import java.util.regex.Pattern;

import show.me.the.money.util.context.Context;
import show.me.the.money.util.exp.ExpRunner;
import show.me.the.money.util.exp.ExpSymbols;
import show.me.the.money.util.exp.Expression;

@SuppressWarnings("unchecked")
public class LIKE extends Expression {
	public LIKE() {
		symbol = ExpSymbols.LIKE;
	}

	public Object run(List lsExp, Context ctx) {
		try {
			List la1 = (List)lsExp.get(1);
			String s1 = (String)ExpRunner.run(la1, ctx);
			List la2 = (List)lsExp.get(2);
			String s2 = (String)(ExpRunner.run(la2, ctx));
			Pattern pattern = Pattern.compile(s2.replaceAll("%", ".*"));
			return pattern.matcher(s1).find();
		} 
		catch (Exception e) {
			logger.fatal("run JSONException:", e);
		}
		return "false";
	}

}
